<%include ../includes/header.html %>
<style type="text/css">
    div#canvas-frame {
        border: none;
        cursor: pointer;
        width: 100%;
        height: 600px;
        background-color: #EEEEEE;
        position: relative;
    }

</style>
    <section class="content-header">
      <h1>
        初级教程
        <small>第8章：动画基础-网格模型旋转的常用技巧和方法</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Junior</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div id="canvas-frame"></div>
    </section>
    <!-- /.content -->
<%include ../includes/footer.html %>
<script src="/bower_components/three/examples/js/controls/TrackballControls.js"></script>
<script>
  $(function(){
 
    var renderer;
    var stats;

    function initThree() {
        width = document.getElementById('canvas-frame').clientWidth;
        height = document.getElementById('canvas-frame').clientHeight;
        renderer = new THREE.WebGLRenderer({
            antialias : true
        });
        renderer.setSize(width, height);
        document.getElementById('canvas-frame').appendChild(renderer.domElement);
        renderer.setClearColor(0xFFFFFF, 1.0);

        stats = new Stats();
        stats.domElement.style.position = 'absolute';
        stats.domElement.style.left = '0px';
        stats.domElement.style.top = '0px';
        document.getElementById('canvas-frame').appendChild(stats.domElement);
    }

    var camera;
    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
        camera.position.x = 100;
        camera.position.y = 300;
        camera.position.z = 600;
        camera.up.x = 0;
        camera.up.y = 1;
        camera.up.z = 0;
        camera.lookAt(0,0,0);

         //用户交互插件 鼠标左键按住旋转，右键按住平移，滚轮缩放
        controls = new THREE.TrackballControls( camera );
        //旋转速度
        controls.rotateSpeed = 5.0;
        //变焦速度
        controls.zoomSpeed = 5;
        //平移速度
        controls.panSpeed = 2;
        //是否不变焦
        controls.noZoom = false;
        //是否不平移
        controls.noPan = false;
        //是否开启移动惯性
        controls.staticMoving = true;
        //动态阻尼系数 就是灵敏度 
        controls.dynamicDampingFactor = 0.3;
    }

    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    var light;
    function initLight() {
        light = new THREE.AmbientLight(0xFF0000);
        light.position.set(100, 100, 200);
        scene.add(light);

    }

    var cube;
    var mesh;
    function initObject() {
       
        var geometry = new THREE.BoxGeometry( 100, 100, 100 );
        for ( var i = 0; i < geometry.faces.length; i += 2 ) {
            var hex = Math.random() * 0xffffff;
            geometry.faces[ i ].color.setHex( hex );
            geometry.faces[ i + 1 ].color.setHex( hex );
        }
        
        // MeshBasicMaterial对光不敏感
        var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors} );
        //material.color = 0xff0000;
        //是否透明，跟opacity联合使用
        material.transparent = true;
        //透明度
        material.opacity = 0.9;
        //侧面渲染方式，如果是立方体的话，就能看到背面轮廓，得配合透明度使用
        material.side = THREE.DoubleSide;
        //是否雾化
        material.fog = false;
        //是否设为线框模式，只看到轮廓
        material.wireframe = false; 

        mesh = new THREE.Mesh( geometry,material);
        mesh.position = new THREE.Vector3(0,0,0);
        scene.add(mesh);
        
        
    }
    
    function initGrid(){
        // 生成了一个网格模型
        var helper = new THREE.GridHelper( 1000, 50, 0x0000ff, 0x808080);
        scene.add( helper );
    }
    
    function threeStart() {
        initThree();
        initCamera();
        initScene();
        initLight();


        initObject();
        initGrid();
        
        animation();

    }

    // 帧循环、游戏循环
    function animation()
    {
        // mesh.rotation.y +=0.01;
        mesh.rotateY(0.01);
        controls.update();
        stats.update();
        renderer.render(scene, camera);
        requestAnimationFrame(animation);

    }

    threeStart();
  })
</script>
